unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, DCPsha256, b32hex, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    od: TFileOpenDialog;
    btnOpen: TButton;
    edtHash: TEdit;
    edtCheck: TEdit;
    btnCheck: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
    procedure btnCheckClick(Sender: TObject);
  private
    { Private declarations }
  public
    function CalculateHash(fileName: String): String;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnCheckClick(Sender: TObject);
begin
  if UpperCase(edtHash.Text) = UpperCase(edtCheck.Text) then
    showMessage('Correct')
  else
    showMessage('Not')

end;

procedure TForm1.btnOpenClick(Sender: TObject);
begin
  if (od.Execute) then
    edtHash.Text := CalculateHash(od.FileName);
end;


function TForm1.CalculateHash;
var
  TestHash: TDCP_sha256;
  Buffer: TKByte;
  TestOut: T32Byte;//array[0..31] of byte;
  s: String;
  fs: TFileStream;
  p: Int64;
begin
  fs := TFileStream.Create(fileName, fmOpenRead);
  TestHash:= TDCP_sha256.Create(nil);


  p := 1024 * (fs.Size div 1024);
  fs.Seek(p, 0);
  fs.Read(Buffer, fs.Size - p);
  //FillChar(TestOut, SizeOf(TestOut), 0);
  TestHash.Init;
  TestHash.Update(Buffer, fs.Size - p);
  TestHash.Final(TestOut);

  while p > 0 do begin
    p := p - 1024;
    fs.Seek(p, 0);
    fs.Read(Buffer, 1024);
    App(TestOut, Buffer);
    TestHash.Init;
    TestHash.Update(Buffer, 1056);
    TestHash.Final(TestOut);
  end;
  Result := VectorToHex(TestOut)
end;

procedure TForm1.FormCreate(Sender: TObject);
{const
  Test1Out: T32Byte=
    ($ba,$78,$16,$bf,$8f,$01,$cf,$ea,$41,$41,$40,$de,$5d,$ae,$22,$23,
     $b0,$03,$61,$a3,$96,$17,$7a,$9c,$b4,$10,$ff,$61,$f2,$00,$15,$ad);
  Test2Out: T32Byte=
    ($24,$8d,$6a,$61,$d2,$06,$38,$b8,$e5,$c0,$26,$93,$0c,$3e,$60,$39,
     $a3,$3c,$e4,$59,$64,$ff,$21,$67,$f6,$ec,$ed,$d4,$19,$db,$06,$c1);
var
  TestHash: TDCP_sha256;
  TestOut: T32Byte;//array[0..31] of byte;
  s: String;    }
begin
{  FillChar(TestOut, SizeOf(TestOut), 0);
  TestHash:= TDCP_sha256.Create(nil);
  TestHash.Init;
  TestHash.UpdateStr(AnsiString('abc'));
  TestHash.Final(TestOut);
  s := VectorToHex(TestOut) + #13#10 + VectorToHex(Test1Out) + #13#10;
  //Result:= boolean(CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out)));
  TestHash.Init;
  TestHash.UpdateStr(AnsiString('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'));
  TestHash.Final(TestOut);
  s := s + VectorToHex(TestOut) + #13#10 + VectorToHex(Test2Out);
  //Result:= boolean(CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out))) and Result;
  TestHash.Free;
  showMessage(s);   }
end;

end.
